# -*- coding:utf-8 -*-

from exscore.parser import Xscores
from datetime import date
from Tkinter import *

class Application(Frame):
    def load(self):
        self.processParsing(self.INPUT.get())
        
    def processParsing(self, url):
    
        #http://www.xscores.com/LiveScore.do?state=promptHockeyTable&sport=4&country=18712,17512,15063,10589,5537,4193,103,113&table=LEAGUE,18712,17512,15063,10589,5537,4193,103,113&league=18730,17530,15081,10590,5538,4194,104,114&season=18779,17569,15120,10592,5540,4196,106,116&year=14030&groupBy=1
        
        self.TEXT.insert(CURRENT, u"Загрузка "+ url +"\n")
        
        parser = Xscores.Xscores(url)
        
        table = parser.getChampTable()
        
        writer = open('table-'+ date.today().strftime("%Y-%m-%d") +'.xls', 'wb')
        writer.write("<table>")
        
        for team in table.getTeams():
            
            self.TEXT.insert(CURRENT, u"Команда "+ team.getName() +"\n")
            
            for game in team.getGames():
                out = []
                out.append(str(game.getNumber()) + ".")
                out.append(str(game.getDate()))
                out.append("<b>" + game.getHomeTeam() + "</b>")
                out.append(game.getGuestTeam())
                out.append(game.getScoreLine().replace('-', 'x'))
                writer.write("<tr><td>" + "</td><td>".join(out) + "</td></tr>")
    
            writer.write("<tr><td></td></tr>")
            
        writer.write("</table>")
        writer.close()
        
        self.TEXT.insert(CURRENT, u"Готово\n\n")
        
    def destroy(self):
        self.quit()


    def createWidgets(self):
        
        self.INPUT = Entry(self, width=50)
        self.INPUT.pack(padx = 1, pady = 1)
        
        self.LOAD = Button(self)
        self.LOAD["text"] = "Загрузить",
        self.LOAD["command"] = self.load
        self.LOAD.pack(padx = 2, pady = 1)
        
        self.TEXT = Text(self, height=10, width=70, background='white')
        scroll = Scrollbar(self)
        self.TEXT.configure(yscrollcommand=scroll.set)
        self.TEXT.pack(side=LEFT)
        scroll.pack(side=RIGHT, fill=Y)


    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.pack()
        self.createWidgets()

    
    
if __name__ == "__main__":
    try:
        root = Tk()
        app = Application(master=root)
        root.title('Exscore')
        app.mainloop()
        root.destroy()
    except:
        pass
    
    